/**
 * $Id$ Copyright 2009-2012 Oak Pacific Interactive. All rights reserved.
 */
package com.shuqi.database.dbimport;

import java.io.File;
import java.util.List;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Environment;
import android.text.TextUtils;

import com.shuqi.application.ShuqiApplication;
import com.shuqi.common.Config;
import com.shuqi.common.LoginHelper;
import com.shuqi.database.dao.impl.AccountDao;
import com.shuqi.database.dao.impl.BookMarkInfoDao;
import com.shuqi.database.model.BookMarkInfo;
import com.shuqi.database.model.UserInfo;


/**
 * 
 * 
 * @author <a href="mailto:">yaozhen</a>
 * @version 2014-3-12上午9:46:55
 */
public class OldDBImprotManager {
    private static OldDBImprotManager mInstance;


    private OldDBImprotManager() {

    }

    public static synchronized OldDBImprotManager getInstance() {
        if (mInstance == null) {
            mInstance = new OldDBImprotManager();
        }
        return mInstance;
    }

    /**
     * 导入7系的数据库
     */
    public void importDB() {
        importUserInfo();
        importBookMarkInfo();
    }

    /**
     * 导入账号信息
     */
    private void importUserInfo() {
        SharedPreferences sp =
                ShuqiApplication.getContext().getSharedPreferences("acount", Context.MODE_PRIVATE);
        String uid = sp.getString("uid", null);
        String nickName = sp.getString("nickname", null);
        String nickSina = sp.getString("nickname_sina", null);
        String sinaKey = sp.getString("tposid_sina", null);
        String email = sp.getString("email", null);
        String session = sp.getString("session", null);
        String gender = sp.getString("gender", null);
        String mobile = sp.getString("mobile", null);
        if (!TextUtils.isEmpty(uid)) {
            UserInfo info = new UserInfo();
            info.setUserId(uid);
            info.setNickName(nickName);
            info.setSinaKey(sinaKey);
            info.setLoginState(UserInfo.LOGINSTATE_LOGINED);
            info.setUserState(UserInfo.USERSTATE_UNCHECK);
            info.setSession(session);
            info.setGender(gender);
            info.setSinaName(nickSina);
            if (!TextUtils.isEmpty(email)) {
                try {
                    info.setEmail(LoginHelper.decrypt(email, Config.ACCOUNTENCRYPTKEY));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if (!TextUtils.isEmpty(mobile)) {
                try {
                    info.setMobile(LoginHelper.decrypt(mobile, Config.ACCOUNTENCRYPTKEY));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            AccountDao.getInstance().saveLoginUserInfo(info);

        }

    }

    /**
     * 导入书签信息
     */
    private void importBookMarkInfo() {
        // 判断db是否存在
        String path =
                Environment.getDataDirectory() + "/data/"+Config.PACKAGENAME_VERIFY+"/databases/shuqi2.db";
        File file = new File(path);
        if (file.exists()) {
            List<BookMarkInfo> list = ImportOldDataHeper.getInstance().readOldBookMarkinfoList();
            BookMarkInfoDao.getInstance().saveOldBookMarks(list);
        }
    }

}
